package com.china08.yxyapi.api.home.index.notice;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNotice;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNoticeRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DisposeCkContentUtil;

/**
 * 首页--公告详情查询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhNoticeDetailController extends YxyApiBaseController {

    @Autowired
    private YxyHomeNoticeRepository yxyHomeNoticeRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/notice/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;

        int status = 1;
        YxyHomeNotice yxyHomeNotice = yxyHomeNoticeRepository.findByIdAndEdbIdAndStatus(reqModel.id, schoolId, status);
        Assert.isTrue(yxyHomeNotice != null, "不存在该内容");

        // 增加浏览量
        yxyHomeNotice.setPv(yxyHomeNotice.getPv() == null ? 0 : yxyHomeNotice.getPv() + 1);
        yxyHomeNoticeRepository.save(yxyHomeNotice);

        String con = yxyHomeNotice.getContent();
        con = DisposeCkContentUtil.disposeHeight(con);
        yxyHomeNotice.setContent(con);

        RespModel respModel = new RespModel();
        respModel.id = yxyHomeNotice.getId();
        respModel.edbId = yxyHomeNotice.getEdbId();
        respModel.status = yxyHomeNotice.getStatus();
        respModel.title = yxyHomeNotice.getTitle();
        respModel.content = yxyHomeNotice.getContent();
        respModel.createdBy = yxyHomeNotice.getCreatedBy();
        respModel.createdDate = DateToolUtils.format(yxyHomeNotice.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
        Date currentDate = yxyHomeNotice.getCreatedDate();
        YxyHomeNotice prevYxyHomeNotice = yxyHomeNoticeRepository
                .findTop1ByEdbIdAndStatusAndCreatedDateGreaterThanOrderByCreatedDateAsc(schoolId, status, currentDate);
        YxyHomeNotice nextYxyHomeNotice = yxyHomeNoticeRepository
                .findTop1ByEdbIdAndStatusAndCreatedDateLessThanOrderByCreatedDateDesc(schoolId, status, currentDate);
        YxyHomeNoticeIdAndTitle prevYxyHomeNoticeIdAndTitle = null;
        YxyHomeNoticeIdAndTitle nextYxyHomeNoticeIdAndTitle = null;
        if (prevYxyHomeNotice != null) {
            prevYxyHomeNoticeIdAndTitle = new YxyHomeNoticeIdAndTitle();
            prevYxyHomeNoticeIdAndTitle.id = prevYxyHomeNotice.getId();
            prevYxyHomeNoticeIdAndTitle.title = prevYxyHomeNotice.getTitle();
        }
        if (nextYxyHomeNotice != null) {
            nextYxyHomeNoticeIdAndTitle = new YxyHomeNoticeIdAndTitle();
            nextYxyHomeNoticeIdAndTitle.id = nextYxyHomeNotice.getId();
            nextYxyHomeNoticeIdAndTitle.title = nextYxyHomeNotice.getTitle();
        }

        respModel.prev = prevYxyHomeNoticeIdAndTitle;
        respModel.next = nextYxyHomeNoticeIdAndTitle;
        return respModel;
    }

    public class YxyHomeNoticeIdAndTitle {
        public String id;
        public String title;
    }

    protected static class RepModel extends YxyApiReqModel {
        // 新闻id
        public String id;
        // 域名
        public String domain;
        // schoolId
        public String schoolId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 新闻id
        public String id;
        // 教育局id
        public String edbId;
        // 发布状态
        public int status;
        // 标题
        public String title;
        // 内容
        public String content;
        // 创建时间
        public String createdDate;

        public String createdBy;
        // 上一条新闻
        public YxyHomeNoticeIdAndTitle prev;
        // 下一条新闻
        public YxyHomeNoticeIdAndTitle next;
    }
}
